//
//  LYMessageActionFloatView.swift
//  WeChat
//
//  Created by mac on 2018/7/25.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa

private let kActionViewWidth: CGFloat = 140
private let kActionViewHeight: CGFloat = 190
private let kActionButtonHeight: CGFloat = 44
private let kFirstButtonY: CGFloat = 12

class LYMessageActionFloatView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    weak var delegate:ActionFloatViewDelegate?
    let disposeBag = DisposeBag()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initContent()
    }
    
    convenience init(){
        self.init(frame: CGRect.zero)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initContent(){
        self.backgroundColor = UIColor.clear
        let actionImages = [
            LYAsset.Contacts_add_newmessage.image,
            LYAsset.Barbuttonicon_add_cube.image,
            LYAsset.Contacts_add_scan.image,
            LYAsset.Receipt_payment_icon.image
        ]
        
        let actionTitle = [
            "发起群聊",
            "添加朋友",
            "扫一扫",
            "收付款"
        ]
        
        let containerView:UIView = UIView()
        containerView.backgroundColor = UIColor.clear
        self.addSubview(containerView)
        containerView.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top).offset(3)
            make.right.equalTo(self.snp.right).offset(-5)
            make.width.equalTo(kActionViewWidth)
            make.height.equalTo(kActionViewHeight)
        }
        
        let stretchInsets = UIEdgeInsetsMake(14, 6, 6, 34)
        let bubbleMaskImage = LYAsset.MessageRightTopBg.image.resizableImage(withCapInsets: stretchInsets, resizingMode: .stretch)
        let bgImageView:UIImageView = UIImageView(image: bubbleMaskImage)
        containerView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(containerView)
        }
        
        var yValue = kFirstButtonY
        for index in 0 ..< actionImages.count{
            let itemButton:UIButton = UIButton(type: .custom)
            itemButton.backgroundColor = UIColor.clear
            itemButton.titleLabel?.font = UIFont.systemFont(ofSize: 17)
            itemButton.setTitleColor(UIColor.white, for: .normal)
            itemButton.setTitleColor(UIColor.white, for: .highlighted)
            itemButton.setTitle(actionTitle[index], for: .normal)
            itemButton.setImage(actionImages[index], for: .normal)
            itemButton.addTarget(self, action: #selector(LYMessageActionFloatView.buttonTaped(_:)), for: .touchUpInside)
            itemButton.contentHorizontalAlignment = .left
            itemButton.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0)
            itemButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
            itemButton.tag = index
            containerView.addSubview(itemButton)
            
            itemButton.snp.makeConstraints { (make) in
                make.top.equalTo(containerView.snp.top).offset(yValue)
                make.right.equalTo(containerView.snp.right)
                make.width.equalTo(containerView.snp.width)
                make.height.equalTo(kActionButtonHeight)
            }
            yValue += kActionButtonHeight
        }
        
        let tap = UITapGestureRecognizer()
        self.addGestureRecognizer(tap)
        tap.rx.event.subscribe { (_) in
            self.hide(true)
        }.disposed(by: self.disposeBag)
        self.isHidden = true
    }
    
    @objc func buttonTaped(_ sender:UIButton){
        guard let delegate = self.delegate else {
            self.hide(true)
            return
        }
        
        let type = ActionFloatViewItemType(rawValue: sender.tag)
        delegate.floatViewTapItemIndex(type!)
        self.hide(true)
    }
    
    func hide(_ hide: Bool) {
        if hide {
            self.alpha = 1.0
            UIView.animate(withDuration: 0.2, animations: {
                self.alpha = 0.0
            }, completion: { finish in
                self.isHidden = true
                self.alpha = 1.0
            })
            
//            UIView.animate(withDuration: 0, animations: {
//
//            }) { (finish) in
//
//            }
            
        }else{
            self.alpha = 1.0
            self.isHidden = false
        }
    }
    
    
    
}

protocol ActionFloatViewDelegate: class {
    func floatViewTapItemIndex(_ type: ActionFloatViewItemType)
}

enum ActionFloatViewItemType: Int {
    case groupChat = 0, addFriend, scan, payment
}
